<?php

namespace app\admin\controller\api;

use app\admin\library\DistinguishAddress;
use app\common\model;

/**
 * 年级
 * @internal
 */
class Grade extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 年级列表
     *
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $list = model\Grade::all();
        $this->success('', $list);
    }

    /**
     * 根据学校id 年级id获取所有班级
     *
     * @param school_id int 学校id
     * @param class_id int 班级id
     * @param type string 类型 sample:1,2,3
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function classes()
    {
        $school_id = $this->request->request('school_id', 0);
        $grade_id = $this->request->request('grade_id', 0);
        $type = $this->request->request('type', 0);
        if ($type) {
            $type = explode(',', $type);
        }

        $list = model\Classes::where('school_id', $school_id)
            ->where('grade_id', $grade_id)
            ->where(function ($query) use ($type) {
                if ($type) {
                    $query->whereIn('type', $type);
                }
            })
            ->field('id,name,type')
            ->select();

        $this->success('', $list);
    }
}